home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13849 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: hubcap.clemson.edu!hubcap!mjs
  2. From: mjs@hubcap.clemson.edu (M. J. Saltzman)
  3. Newsgroups: comp.programming,comp.lang.c
  4. Subject: Re: A weird thing about printf()
  5. Followup-To: comp.lang.c
  6. Date: 10 Apr 96 14:49:46 GMT
  7. Organization: Clemson University
  8. Message-ID: <mjs.829147786@hubcap>
  9. References: <4kflru$5if@dewey.csun.edu>
  10. NNTP-Posting-Host: hubcap.clemson.edu
  11. X-Newsreader: NN version 6.5.0 #1
  12.  
  13. This question properly belongs in comp.lang.c, to which I have directed
  14. followups.
  15.  
  16. kc44097@csun.edu (chen) writes:
  17.  
  18. >  The program :
  19.  
  20. >#include <stdio.h>
  21.  
  22. >int answer;
  23. >main()
  24. >{
  25. >  answer=2+2;
  26. >  printf("The answer is %d\n");
  27. >  return 0;
  28. >}
  29.  
  30. >  In printf(),I lost "answer" in the end,but it works,and give me the
  31. >result 0.I wonder how the compiler handle this condition.Also,is this
  32.  
  33. It doesn't "work", since it doesn't give the answer you are looking
  34. for.  What probably happens is that printf() looks where the second
  35. argument *would be* and prints whatever random garbage happens to be
  36. there, but what's allowed to happen is anything at all, i.e., the
  37. behavior is undefined.
  38.  
  39. >allow by C?(by this, I mean is this a leagal usage in language itself,
  40. >or just a mistake cause by the compiler?)
  41.  
  42. It's a mistake caused by the programmer!  Since printf() is declared
  43. to take a variable number of arguments, the compiler has no obligation
  44. to check the number of arguments.  Some compilers know that printf()
  45. is special and check the argument count against the format string, but
  46. that feature is not all that common (and can only be applied if the
  47. format string is a constant anyway).
  48.  
  49. >  Can someone give me some advice ? Please e-mail me :
  50. >                   kc44097@huey.csun.edu
  51.  
  52. >                 Thankx !!!!!!
  53. -- 
  54.         Matthew Saltzman
  55.         Clemson University Math Sciences
  56.         mjs@clemson.edu
  57.